package com.example.take_out_admin.service.impl;

import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.take_out_admin.common.Result;
import com.example.take_out_admin.config.JwtConfig;
import com.example.take_out_admin.entity.Employee;
import com.example.take_out_admin.exceptions.BusinessException;
import com.example.take_out_admin.exceptions.enumetation.ResponseEnum;
import com.example.take_out_admin.mapper.EmployeeMapper;
import com.example.take_out_admin.service.EmployeeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.take_out_admin.utils.JwtUtils;
import io.jsonwebtoken.Claims;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 员工信息 服务实现类
 * </p>
 *
 * @author yyyyys
 * @since 2024-06-29
 */
@Slf4j
@Service
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements EmployeeService {
    @Resource
    private JwtConfig jwtConfig;
    @Resource
    private HttpServletRequest request;

    @Override
    public String login(Employee employee) {
        LambdaQueryWrapper<Employee> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Employee::getUsername,employee.getUsername());

        Employee one = this.getOne(wrapper);
        if (one == null){
            log.info("用户名：{}，不存在",employee.getUsername());
            throw new RuntimeException("用户名或密码不正确");
        }
        String password = DigestUtil.md5Hex(employee.getPassword().getBytes());
        if (!one.getPassword().equals(password)){
            log.info("用户名：{}，密码：{}不正确",employee.getUsername(),employee.getPassword());
            throw new RuntimeException("用户名或密码不正确");
        }
        String token = JwtUtils.createJwt(
                one.getId(),
                one.getUsername(),
                "",
                jwtConfig.getTtl(),
                jwtConfig.getKey()
        );
        log.info("token:{}",token);
        return token;
    }

    @Override
    public void updateEmployee(Employee employee) {
        String token = request.getHeader("token");
        log.info("token{}",token);
        if (!JwtUtils.validateToken(token,jwtConfig.getKey())){
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }
        Claims claims = JwtUtils.parseJwt(token, jwtConfig.getKey());
        String name = claims.get("name", String.class);
        employee.setCreateUser(name);
        employee.setUpdateUser(name);
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        this.updateById(employee);
    }

    @Override
    public void saveEmployee(Employee employee) {
        String token = request.getHeader("token");
        log.info("token:{}",token);
        if (!JwtUtils.validateToken(token,jwtConfig.getKey())){
            throw new BusinessException(ResponseEnum.TOKEN_EXPIRED);
        }
        Claims claims = JwtUtils.parseJwt(token, jwtConfig.getKey());
        String name = claims.get("name", String.class);
        employee.setCreateUser(name);
        employee.setUpdateUser(name);
        employee.setCreateTime(new Date());
        employee.setUpdateTime(new Date());
        save(employee);
    }

    @Override
    public boolean validateToken(String token) {
        return JwtUtils.validateToken(token,jwtConfig.getKey());
    }




}
